package com.xingkeduo.business.operation.controller.api;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.operation.controller.InitController;
import com.xingkeduo.business.operation.domain.HairstyleDomain;
import com.xingkeduo.pojo.dto.Hairstyle.ChooseHairstyleDTO;
import com.xingkeduo.pojo.dto.Hairstyle.HairstyleDTO;
import com.xingkeduo.pojo.dto.Hairstyle.HairstyleDetailResponseDTO;
import com.xingkeduo.pojo.dto.Hairstyle.HairstyleResponseDTO;
import com.xingkeduo.pojo.json.HairstyleJson;
import com.xingkeduo.pojo.query.HairstyleQuery;
import com.xingkeduo.pojo.table.HairstyleTable;
import com.xingkeduo.tool.ResponseUtils;
import com.xingkeduo.util.pojo.domain.Page;
import com.xingkeduo.util.pojo.exception.SystemException;
import com.xingkeduo.util.tool.ReturnUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.xingkeduo.tool.OperaConstants.API_URL_PREFIX;

/**
 * 发型库管理
 *
 * @author chenxiangda <chenxiangda@xingkeduo.com>
 * @version id:v0.1
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@RestController
@RequestMapping(API_URL_PREFIX +"/hairstyle")
public class HairstyleApiController extends InitController {

    @Autowired
    private HairstyleDomain hairstyleDomain;

    /**
     * id查询
     * @param urlParam
     * @return
     */
    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
    public ResponseUtils doGet(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            HairstyleQuery hairstyleQuery = HairstyleJson.createQueryForHairstyle(urlParam);
            long id = hairstyleQuery.getId();
            HairstyleDetailResponseDTO hairstyleDetailResponseDTO = hairstyleDomain.getHairstyleInfoApiById(id);
            return ResponseUtils.getSuccess(hairstyleDetailResponseDTO);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }

    /**
     * 发型列表
     * @param urlParam
     * @return
     */
    @RequestMapping(value = "/lists/{urlParam:.+}", method = RequestMethod.GET)
    public ResponseUtils doGetLists(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            HairstyleQuery hairstyleQuery = new HairstyleQuery();
            hairstyleQuery=  JSON.parseObject(urlParam,HairstyleQuery.class);
            List<HairstyleResponseDTO> hairstyleApilists = hairstyleDomain.getHairstyleApilists(hairstyleQuery);
            return ResponseUtils.getSuccess(hairstyleApilists);

        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }

    /**
     * 发型带分页列表小程序
     * @param
     * @return
     */

    @ResponseBody
    @RequestMapping(value = "/hairstyleLists/{urlParam:.+}", method = RequestMethod.GET)
//    @RequestMapping(value = "/hairstyleLists", method = RequestMethod.POST)
    public ResponseUtils doGetPageLists(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            HairstyleQuery hairstyleQuery = new HairstyleQuery();
            hairstyleQuery=  JSON.parseObject(urlParam,HairstyleQuery.class);
            Page<HairstyleTable> hairstylePageList = hairstyleDomain.getHairstylePageList(hairstyleQuery);
            return ResponseUtils.getSuccess(hairstylePageList);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }


    /**
     *选择发型列表
     */
    @ResponseBody
    @RequestMapping(value = "/hairstyleList", method = RequestMethod.GET)
    public ResponseUtils doGetList(HairstyleQuery hairstyleQuery) {
        returnUtil = new ReturnUtil();
        try {
            List<ChooseHairstyleDTO> hairstyleApilists = hairstyleDomain.getHairstyleApilist(hairstyleQuery);
            return ResponseUtils.getSuccess(hairstyleApilists);

        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }
}
